package com.chenjun.core.service.sys.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysDictionary;
import com.chenjun.core.dao.generator.mapper.SysDictionaryMapper;
import com.chenjun.core.dao.module.SysDictionaryRelationMapper;
import com.chenjun.core.service.sys.SysDictionaryService;

/**
 * 
 * @Description:  字典服务
 * @author yanjinyin
 * @date 2017年7月29日 下午2:05:14
 */
@SuppressWarnings("serial")
@Service
@Transactional
public class SysDictionaryServiceImpl implements SysDictionaryService{

	private static final String SYS_DICTIONARY_MAPPER = "SysDictionaryMapper";
    private static final Logger logger = Logger.getLogger(SysDictionaryServiceImpl.class);

    @Autowired
    private SysDictionaryMapper sysDictionaryMapper;
    
    @Autowired
    private SysDictionaryRelationMapper sysDictionaryRelationMapper;

	public SysDictionary get(String id){
		return sysDictionaryMapper.selectByPrimaryKey(id);
	}

	public int create(SysDictionary record){
		return sysDictionaryMapper.insert(record);
	}

	public int delete(String id){
		return sysDictionaryMapper.deleteByPrimaryKey(id);
	}

	public int update(SysDictionary record){
		return sysDictionaryMapper.updateByPrimaryKey(record);
	}

	public int createOrUpdate(SysDictionary record){
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			record.setCreatedTime(Calendar.getInstance().getTime());
			rows = sysDictionaryMapper.insert(record);
		} else {
			rows = sysDictionaryMapper.updateByPrimaryKeySelective(record);
		}
		return rows;
	}

	public int doCountByCondition(DataGridModel param){
		return sysDictionaryRelationMapper.countByCondition(param);
	}

	public List<SysDictionary> doListByCondition(DataGridModel param){
		return sysDictionaryRelationMapper.selectByCondition(param);
	}

	public Pagination doPagination(DataGridModel param){
		Pagination vo = new Pagination();
		List<SysDictionary> list = sysDictionaryRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysDictionaryRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}

	/**
	 * 根据type查询字典表数据
	 * 
	 * @author Luo
	 * @param type
	 * @return
	 */
	public List<SysDictionary> getSelections(String type) {
		
		return sysDictionaryRelationMapper.selectByType(type);
	}
}